﻿using System;
using System.Collections.Generic;
using System.Linq;

class SelectionSort
{
    static void Main()
    {
        Console.Write("Number of elements of array:");
        int arrayLength = int.Parse(Console.ReadLine());
        List<int> array = new List<int>();
        for (int i = 0; i < arrayLength; i++)
        {
            Console.Write("\nInt element a[{0}]:", i);
            array.Add(int.Parse(Console.ReadLine()));
        }
        //List<int> array = new List<int> { 3, 2, 3, 9, 6, 1, 4, 7, 20, 15, 6, 8, 4, 2, 2, 4 };
        //int arrayLength = 16;

        int[] sortedArray = new int[arrayLength];
        int index = 0;

        int candidateSmallestNumber = new int();
        int candidateSmallestNumberPosition = new int();
        while (array.Count != 0)
        {
            candidateSmallestNumber = array[0];
            candidateSmallestNumberPosition = 0;
            for (int i = 1; i < array.Count; i++)
            {
                if (candidateSmallestNumber >= array[i])
                {
                    candidateSmallestNumber = array[i];
                    candidateSmallestNumberPosition = i;
                }
            }
            sortedArray[index] = candidateSmallestNumber;
            array.RemoveAt(candidateSmallestNumberPosition);
            index++;
        }

        Console.Write("The array sorted is: "+sortedArray[0]);
        for (int i = 1; i < sortedArray.Length; i++)
        {
            Console.Write(", " + sortedArray[i]);
        }
        Console.WriteLine();
    }
}